package cn.hengzhu.main_manager.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hengzhu.main_manager.repository.dao.ManageApkMapper;
import cn.hengzhu.main_manager.repository.domain.ManageApk;
import cn.hengzhu.main_manager.repository.domain.ManageApkExample;
import cn.hengzhu.main_manager.repository.param.PageBaseParam;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.AliOssUploadUtil;
import cn.hengzhu.main_manager.utils.LayUiResultJson;
import cn.hengzhu.main_manager.utils.config.AliOssConfig;
import net.dongliu.apk.parser.ByteArrayApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;

@Controller
@RequestMapping("/myApk")
public class MyApkController {
	@Autowired
	private ManageApkMapper manageApkMapper;
	
	
	@RequestMapping("/index")
	public String index(){
		return "apkList";
	}

	
	@RequestMapping("/getApkList")
	@ResponseBody
	public LayUiResultJson<ManageApk> getApkList(PageBaseParam param){
		PageHelper.startPage(param.getPage(), param.getLimit(), true);
		ManageApkExample manageApkExample=new ManageApkExample();
		manageApkExample.setOrderByClause("CREATE_TIME desc");
		manageApkExample.createCriteria().andIsDelEqualTo((byte) 0);
		List<ManageApk> list = manageApkMapper.selectByExample(manageApkExample);
		PageInfo<ManageApk> pageInfo = new PageInfo<>(list);
		list.stream().forEach(a->{
			a.setUrl(AliOssConfig.URL+a.getUrl());
		});
		
		long count = pageInfo.getTotal();
		LayUiResultJson<ManageApk> result = new LayUiResultJson<>(count, list);
		return result;
		
	}
	
	
	/**
	 * @author liuCheng
	 * @since 2018年11月21日 下午2:42:13 
	 * @param file
	 * @return
	 * @throws IOException
	 * 上传
	 */
	@RequestMapping ("/uploadFile")
    @ResponseBody
    public Map<String ,String> upFile(MultipartFile file) throws IOException {
		Map<String ,String> map=new HashMap<>();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String fileName = uuid + ".apk";
		ManageApk manageApk=new ManageApk();
		ByteArrayApkFile byteArrayApkFile = new ByteArrayApkFile(file.getBytes());
		ApkMeta apkMeta = byteArrayApkFile.getApkMeta();
		String packageName = apkMeta.getPackageName();
		Long versionCode = apkMeta.getVersionCode();
		String versionName = apkMeta.getVersionName();
		manageApk.setCreateTime(new Date());
		manageApk.setPackageName(packageName);
		manageApk.setVersionCode(versionCode.toString());
		manageApk.setVersionName(versionName);
		manageApk.setUrl(fileName);
		try {
			String upLoad = AliOssUploadUtil.upLoad(file,fileName);
			manageApkMapper.insertSelective(manageApk);
			if(StringUtils.isNotBlank(upLoad)){
				map.put("code", "100");
				map.put("fileName", fileName);
			}else{
				map.put("code", "101");
			}
		} catch (Exception e) {
			map.put("code", "101");
		}finally {
			byteArrayApkFile.close();
		}
		return map;
    }
	
	/**
	 * @author liuCheng
	 * @since 2018年11月21日 下午2:52:39 
	 * @param param
	 * @return
	 * 删除
	 */
	@RequestMapping("/deleteApk")
	@ResponseBody
	public AjaxResult deleteApk(Integer id){
		
		ManageApk record=new ManageApk();
		record.setIsDel((byte) 1);
		record.setId(id);
		try {
			manageApkMapper.updateByPrimaryKeySelective(record);
			return new AjaxResult();
		} catch (Exception e) {
			return new AjaxResult("删除失败");
		}
		
		
	}
	
	

}
